<!DOCTYPE html>
<!--suppress ThymeleafVariablesResolveInspection -->
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <div th:replace="header::common_resource"></div>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">类别名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入类别名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择父类</label>
            <div class="layui-input-inline">
                <select name="parentId" id="TopCategoryId" lay-filter="TopCategoryId">
                    <option value="0">无父类</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" onclick="submitForm()" class="layui-btn" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script>

        $(function () {
            $.post(
                '/category/selectTopCategory',
                function (result) {
                    $(result.data).each(function () {
                        $('#TopCategoryId').append('<option value="' + this.id + '">' + this.name + '</option>');
                    });
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render('select');
                    });
                },
                'json'
            );
        });

        function submitForm() {
            $.post(
                '/category/insert',
                $('#formId').serialize(),
                function(result) {
                    console.log(result);
                    if (result.ok) {
                        mylayer.okMsg(result.msg)
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    } else {
                        mylayer.errorMsg(result.msg);
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>